/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-07-10
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.*;
import com.rzico.basics.mapper.ReceiverMapper;
import com.rzico.basics.mapper.ShopMapper;
import com.rzico.basics.model.MapPoint;
import com.rzico.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.MapLineMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 送货线路业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class MapLineService extends BaseServiceImpl<MapLine, String> {

    @Autowired
    private MapLineMapper mapLineMapper;

    @Autowired
    private ReceiverMapper receiverMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public BaseMapper<MapLine, String> getMapper() {

       return mapLineMapper;

    }

    @Transactional(rollbackFor = Exception.class)
    public int save(MapLine mapLine) {

       return mapLineMapper.insertUseGeneratedKeys(mapLine);

    }

    @Transactional(rollbackFor = Exception.class)
    public int update(MapLine mapLine) {

       return mapLineMapper.updateByPrimaryKeySelective(mapLine);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Object[] ids) {
        int rw = 0;
        for (Object id:ids) {
            Map<String,Object> params = new HashMap<>();
            params.put("mapLineId",id);
            Integer c = receiverMapper.selectRowCount(params);
            if (c>0) {
               throw new CustomException("在使用不能删除");
            }
            rw = rw + super.deleteByPrimaryKey(id);
        }
        return rw;
    }

    //本企业最近的线路
    public MapLine findByPoint(Enterprise enterprise,Long shopId, Double lat, Double lng) {

        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",enterprise.getId());
        if (shopId!=null) {
            params.put("shopId",shopId);
        }
        List<MapLine> mapLineList = selectList(params);
        if (mapLineList.size()==0) {
            return new MapLine();
        }
        MapPoint point = new MapPoint();
        point.setLat(lat);
        point.setLng(lng);

        MapLine mapLine = null;
        Double d = 0d;

        //没有维护线路时按店铺最近距离
        for (MapLine ml:mapLineList) {

            if (point.inPolygon(ml.getMapPoint())) {
                Shop shop = shopMapper.selectByPrimaryKey(ml.getShopId());
                //在配送范围，判断离谁近
                Double dt = 9999999d;
                if (shop.getLat()==null) {
                    shop.setLat(0d);
                }
                if (shop.getLng()==null) {
                    shop.setLng(0d);
                }
                if (shop.getLat()>0 && shop.getLat()>0) {
                    MapPoint shopPoint = new MapPoint();
                    shopPoint.setLat(shop.getLat());
                    shopPoint.setLng(shop.getLng());
                    dt = point.distatce(shopPoint);
                }
                if (d.compareTo(0d)==0) {
                    d = dt;
                    mapLine = ml;
                } else {
                    if (d.compareTo(dt)>0) {
                        d = dt;
                        mapLine = ml;
                    }
                }

            }

        }

        return mapLine;

    }

    //在服务商内搜索最近的线路
    public MapLine findByISV(Enterprise enterprise, Double lat, Double lng) {

        Map<String,Object> params = new HashMap<>();
        params.put("isvId",enterprise.getMchId());
        List<MapLine> mapLineList = selectList(params);

        MapPoint point = new MapPoint();
        point.setLat(lat);
        point.setLng(lng);

        MapLine mapLine = null;
        Double d = 0d;
        for (MapLine ml:mapLineList) {

            if (point.inPolygon(ml.getMapPoint())) {
                Shop shop = shopMapper.selectByPrimaryKey(ml.getShopId());
                if (shop.getStatus().equals(1)) {
                    continue;
                }
                //在配送范围，判断离谁近
                Double dt = 9999999d;
                if (shop.getLat()==null) {
                    shop.setLat(0d);
                }
                if (shop.getLng()==null) {
                    shop.setLng(0d);
                }
                if (shop.getLat()>0 && shop.getLat()>0) {
                    MapPoint shopPoint = new MapPoint();
                    shopPoint.setLat(shop.getLat());
                    shopPoint.setLng(shop.getLng());
                    dt = point.distatce(shopPoint);
                }
                if (d.compareTo(0d)==0) {
                    d = dt;
                    mapLine = ml;
                } else {
                    if (d.compareTo(dt)>0) {
                        d = dt;
                        mapLine = ml;
                    }
                }

            }

        }

        return mapLine;

    }

}
